package kr.co.icebird.temp;

import java.security.MessageDigest;

import sun.misc.BASE64Encoder;

public class SHA {
	private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();

	public byte[] digest(byte[] data, int length) throws Exception {
		MessageDigest md = MessageDigest.getInstance("SHA-1");
		md.update(data, 0, length);
		byte[] raw = md.digest();
		return raw;
	}

	public static void main(String[] args) throws Exception {
		SHA aa = new SHA();

		String input = "S_CMS_CP";
		String test0 = "011c945f30ce2cbafc452f39840f025693339c42";
		String test1 = "e8b6d984affc0babbf3582bbad3202b2663472e2";
		String test2 = "2702c9687980a4d13bb1d6cc872021b5e3e01df9";
		BASE64Encoder encoder = new BASE64Encoder();
		byte[] digest = aa.digest(input.getBytes(), input.length());
		String base64 = encoder.encode(digest);

		System.out.println(toString(digest, 0, digest.length));
		
		System.out.println(test1.equals(toString(digest, 0, digest.length)));
	}

	public static final String toString(byte[] ba, int offset, int length) {
		char[] buf = new char[length * 2];
		for (int i = 0, j = 0, k; i < length;) {
			k = ba[offset + i++];
			buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
			buf[j++] = HEX_DIGITS[k & 0x0F];
		}
		return new String(buf);
	}
}
